package com.gy.zpchat.ui.conversationlist;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMConversationListQueriedCallback;
import im.zego.zim.callback.ZIMConversationPinnedListQueriedCallback;
import im.zego.zim.entity.ZIMConversation;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.ui.BaseActivity;

public class ConversationListActivity extends BaseActivity implements ZIMConversationListQueriedCallback {
    private final ConversationListAdapter conversationListAdapter = new ConversationListAdapter();
    private static final int QUERY_CONVERSATION_COUNT = 20;
    private boolean isLoading = false;

    public static String FLAG_NEED_RESULT = "needResult";
    public static String FLAG_IS_PINNED = "isPinned";
    public static String FLAG_RESULT_CONVERSATION_ID = "conversationID";
    public static String FLAG_RESULT_CONVERSATION_TYPE = "conversationType";
    public static String FLAG_RESULT_CODE = "resultCode ";
    private boolean isPinned = false;
    private boolean needResult = false;
    private int resultCode = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_pinned_list);

        RecyclerView conversationRecyclerView = findViewById(R.id.conversation_list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        conversationRecyclerView.setLayoutManager(linearLayoutManager);
        conversationRecyclerView.setAdapter(conversationListAdapter);
        conversationRecyclerView.setItemAnimator(new DefaultItemAnimator());

        isPinned = getIntent().getBooleanExtra(FLAG_IS_PINNED,false);
        needResult = getIntent().getBooleanExtra(FLAG_NEED_RESULT,false);
        resultCode = getIntent().getIntExtra(FLAG_RESULT_CODE,0);

        setItemClick();
        conversationRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == conversationListAdapter.getItemCount()) {
                    if (!isLoading) {
                        isLoading = true;
                        loadData();
                    }
                }
            }
        });
        loadData();
    }
    private void setItemClick(){
        conversationListAdapter.setOnItemClickListener(new ConversationListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ZIMConversation conversation) {
                if (needResult){
                    Intent intent = new Intent();
                    intent.putExtra(FLAG_RESULT_CONVERSATION_ID,conversation.conversationID);
                    intent.putExtra(FLAG_RESULT_CONVERSATION_TYPE,conversation.type.value());
                    setResult(resultCode,intent);
                    finish();
                }
            }
        });

    }
    private void loadData(){
        if (isPinned){
            loadConversationPinnedList();
        }else {
            loadConversationList();
        }
    }

    private void loadConversationList() {
        SDKManager.sharedInstance().queryConversationList(conversationListAdapter.getLastConversation(), QUERY_CONVERSATION_COUNT, this);
    }

    @Override
    public void onConversationListQueried(ArrayList<ZIMConversation> conversationList, ZIMError errorInfo) {
        if (errorInfo.code != ZIMErrorCode.SUCCESS) {
            Toast.makeText(this, "Failed to query conversation, error:" + errorInfo.code, Toast.LENGTH_SHORT).show();
            return;
        }
        for (ZIMConversation conversation : conversationList) {
            conversationListAdapter.addOrUpdate(conversation);
        }
        conversationListAdapter.sortAndNotify();
        isLoading = false;
    }

    public static void actionStart(Activity activity,boolean isPinned) {
        Intent intent = new Intent(activity, ConversationListActivity.class);
        intent.putExtra(FLAG_IS_PINNED,isPinned);
        activity.startActivity(intent);
    }

    private void loadConversationPinnedList() {
        int count = 30;
        SDKManager.sharedInstance().queryConversationPinnedList(conversationListAdapter.getLastConversation(), count, new ZIMConversationPinnedListQueriedCallback(){
                @Override
            public void onConversationPinnedListQueried(ArrayList<ZIMConversation> conversationList, ZIMError errorInfo) {
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ConversationListActivity.this, "Failed to onConversationPinnedListQueried, error:" + errorInfo.code, Toast.LENGTH_SHORT).show();
                    return;
                }
                for (ZIMConversation conversation : conversationList) {
                    conversationListAdapter.addOrUpdate(conversation);
                }
                conversationListAdapter.sortAndNotify();
                isLoading = false;
            }
        });
    }


}